

#import "HNurseAssessFormViewController.h"
#import "HCommon.h"

@interface HNurseAssessFormViewController ()

@property (nonatomic,strong) HQc_codeModel *theCodeModel;
@property (nonatomic,strong) Model_Hr_InspectDetail_New *theEditData;
@property (nonatomic,strong) NSIndexPath *indexPath;

@end

@implementation HNurseAssessFormViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    UIBarButtonItem *savebtn = [[UIBarButtonItem alloc] initWithTitle:@"保存" style:UIBarButtonItemStylePlain target:self action:@selector(didSaveButttonClicked:)];
    self.navigationItem.rightBarButtonItem = savebtn;
    
    UIBarButtonItem *cancelbtn = [[UIBarButtonItem alloc] initWithTitle:@"取消" style:UIBarButtonItemStylePlain target:self action:@selector(didCancelButttonClicked:)];
    self.navigationItem.leftBarButtonItem = cancelbtn;
    
    // 监听键盘消息
    
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillShow:)
                                                 name:UIKeyboardWillShowNotification
                                               object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillHide:)
                                                 name:UIKeyboardWillHideNotification
                                               object:nil];
    
    [self.view addSubview: [self GetFormView ]];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


-(IBAction)keyboardWillShow:(id)sender{
  
    UIScrollView *scr = self.view.subviews[0];
    scr.contentOffset = CGPointMake(0, 80);

}
-(IBAction)keyboardWillHide:(id)sender{
    UIScrollView *scr = self.view.subviews[0];
    scr.contentOffset = CGPointZero;
}

-(NSDictionary*) GetFormData{
   
    
    //  扣分 扣分原因
    UIScrollView *scrv = self.view.subviews[0];
    UITextField *kft = scrv.subviews[3];
    UITextView *kfyyt = scrv.subviews[5];
    
    NSString *zbid = self.theCodeModel.CODE_ID;
    NSString *kf = [ NSString stringWithFormat:@"%.1f",[kft.text floatValue] ];
    NSString *kfyy = kfyyt.text;
    
    CGFloat maxscore = [self.theCodeModel.SCORE floatValue];
    CGFloat dfz = maxscore - [kf floatValue];
    NSString *df = [NSString stringWithFormat:@"%.1f",dfz];
    
    NSDictionary *dict = [[NSDictionary alloc ] initWithObjectsAndKeys:zbid,@"CODE_ID",kf,@"KF",kfyy,@"KFYY",df,@"DF", nil];
    
    return dict;
}


-(IBAction)didSaveButttonClicked:(id)sender{
    
    
    NSDictionary *dict = [self GetFormData];
    CGFloat kf = [[dict objectForKey:@"KF"] floatValue];
    if(kf<0){
        [HCommon popupErrorMessage:@"扣分不允许为负数!"];
        return;
    }
    else if(kf > 0) {
        
        if(kf > [self.theCodeModel.SCORE floatValue]){
            [HCommon popupErrorMessage:@"您输入的扣分值大于本项分值，请确认!"];
            return;
        }
        
        NSString *kfyy = [dict objectForKey:@"KFYY"];
        if( [kfyy isEqualToString:@""]){
            [HCommon popupErrorMessage:@"请填写扣分原因!"];
            return;
        }
    }
    
    [self.delegate CallBackWithIndexPath:dict NSIndexPath:self.indexPath];
    [self.navigationController dismissViewControllerAnimated:true completion:nil];
    
}
-(IBAction)didCancelButttonClicked:(id)sender{
    [self.navigationController dismissViewControllerAnimated:true completion:nil];
}


-(instancetype)initWithCodeModel: (HQc_codeModel *)code NSIndexPath : (NSIndexPath *)indexPath Model_Hr_InspectDetail_New:(Model_Hr_InspectDetail_New*)detailModel{
    
    self.theCodeModel = code;
    self.indexPath = indexPath;
    self.theEditData = detailModel;
    return self;
}

-(UIView *) GetFormView{

    UIScrollView *main = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight)];
    main.backgroundColor = UIColor.groupTableViewBackgroundColor;
    main.contentSize = CGSizeMake(ScreenWidth, ScreenHeight);
    
    CGFloat separatedHeight = 10;
    
    // 0
    UILabel *zbmc = [[UILabel alloc]initWithFrame:CGRectMake(10, 10, ScreenWidth - 20, 60)];
    zbmc.lineBreakMode = NSLineBreakByWordWrapping;
    zbmc.numberOfLines = 0;
    zbmc.text = [NSString stringWithFormat:@"项目: %@", self.theCodeModel.CODE_NAME];
    //zbmc.textColor = RGB(0x99,0x00,0x00);
    [main addSubview: zbmc];
    
    // 1
    UILabel *fz = [[UILabel alloc]initWithFrame:CGRectMake(10, zbmc.bottom + separatedHeight, ScreenWidth - 20, 30)];
    fz.text = [NSString stringWithFormat:@"分值: %@", self.theCodeModel.SCORE];
    [main addSubview:fz];
    
    // 2
    UILabel *kf = [[UILabel alloc]initWithFrame:CGRectMake(10, fz.bottom + separatedHeight, 140, 30)];
    kf.text =  @"扣分: ";
    [main addSubview:kf];
    
    // 3
    UITextField *kfField =  [[UITextField alloc]initWithFrame:CGRectMake(kf.right + 10, fz.bottom + separatedHeight, ScreenWidth - 140 - 30, 30)];
    kfField.text = @"0";
    kfField.textAlignment = NSTextAlignmentCenter;
    kfField.layer.cornerRadius = 5;
    kfField.layer.borderColor = [RGB(0xff,0xcc,0x99) CGColor];
    kfField.backgroundColor = UIColor.whiteColor;
    kfField.layer.borderWidth = 0.2;
    kfField.keyboardType = UIKeyboardTypeDecimalPad;
    kfField.delegate = self;
    kfField.text = F(@"%0.1f",([self.theCodeModel.SCORE floatValue] -  self.theEditData.SCORE));
    
    [kfField setInputAccessoryView:[super getDismissKeyboradToolbar]];
    
    [main addSubview:kfField];
    
    
    // 4
    UILabel *kfyy = [[UILabel alloc]initWithFrame:CGRectMake(10, kf.bottom + separatedHeight, ScreenWidth - 20, 30)];
    kfyy.text =  @"扣分原因: ";
    [main addSubview:kfyy];
    
    
    // 5
    UITextView *kfyyField =  [[UITextView alloc]initWithFrame:CGRectMake(10, kfyy.bottom + separatedHeight, ScreenWidth - 20, 90)];
    kfyyField.layer.cornerRadius = 5;
    kfyyField.layer.borderColor = [RGB(0xff,0xcc,0x99) CGColor];
    kfyyField.backgroundColor = UIColor.whiteColor;
    kfyyField.layer.borderWidth = 0.2;
    kfyyField.keyboardType = UIKeyboardTypeDefault;
    [kfyyField setInputAccessoryView:[super getDismissKeyboradToolbar]];
    kfyyField.text = self.theEditData.RESION;
    
    [main addSubview:kfyyField];
    
    
    
    
    
    return main;
}

-(void)MyDismissKeyboardSelector
{
    [super MyDismissKeyboardSelector];
    [super dismissKeyboardWithParentView:self.view.subviews[0]];
}

#pragma mark TextFiled Delegate



- (void)textFieldDidBeginEditing:(UITextField *)textField{
    if ([textField.text floatValue] == 0){
        textField.text = @"";
    }
    
    [textField setNeedsDisplay];
}

- (void)textFieldDidEndEditing:(UITextField *)textField{

    NSString *txtvalue = textField.text;
    CGFloat fvalue = [txtvalue floatValue];
    
    CGFloat maxscore = [self.theCodeModel.SCORE floatValue];
    
    if(maxscore < fvalue){
        [HCommon popupErrorMessage:@"所填写的分值比该项目分值高，将设置为最高分"];
        textField.text = [ NSString stringWithFormat:@"%.1f",maxscore ];
    }
    
    [textField setNeedsDisplay];
}


@end
